package com.poker.monitor.recognition;

import android.graphics.Bitmap;
import android.graphics.Rect;
import java.util.ArrayList;
import java.util.List;

public class RegionRecognizer {
    private static final int PLAYER_COUNT = 8;
    private List<Rect> playerRegions;
    private Rect communityCardsRegion;
    private Rect potRegion;

    public RegionRecognizer() {
        playerRegions = new ArrayList<>();
        initializeRegions();
    }

    private void initializeRegions() {
        // 初始化各个区域的位置
        // 这里的坐标值需要根据实际游戏界面进行调整
        for (int i = 0; i < PLAYER_COUNT; i++) {
            // 计算每个玩家区域的位置
            Rect playerRegion = calculatePlayerRegion(i);
            playerRegions.add(playerRegion);
        }

        // 设置公共牌区域
        communityCardsRegion = new Rect(/* x1, y1, x2, y2 */);
        // 设置底池区域
        potRegion = new Rect(/* x1, y1, x2, y2 */);
    }

    private Rect calculatePlayerRegion(int playerIndex) {
        // 根据玩家位置计算区域坐标
        // 这里需要根据实际游戏界面布局计算具体坐标
        return new Rect(/* x1, y1, x2, y2 */);
    }

    public List<Bitmap> extractPlayerRegions(Bitmap screenshot) {
        List<Bitmap> playerImages = new ArrayList<>();
        for (Rect region : playerRegions) {
            Bitmap playerImage = Bitmap.createBitmap(
                screenshot,
                region.left,
                region.top,
                region.width(),
                region.height()
            );
            playerImages.add(playerImage);
        }
        return playerImages;
    }

    public Bitmap extractCommunityCardsRegion(Bitmap screenshot) {
        return Bitmap.createBitmap(
            screenshot,
            communityCardsRegion.left,
            communityCardsRegion.top,
            communityCardsRegion.width(),
            communityCardsRegion.height()
        );
    }

    public Bitmap extractPotRegion(Bitmap screenshot) {
        return Bitmap.createBitmap(
            screenshot,
            potRegion.left,
            potRegion.top,
            potRegion.width(),
            potRegion.height()
        );
    }

    public void updateRegions(int screenWidth, int screenHeight) {
        playerRegions.clear();
        
        // 更新玩家区域
        // 上排玩家
        playerRegions.add(new Rect(screenWidth / 4, 0, screenWidth / 3, screenHeight / 4));           // 玩家1
        playerRegions.add(new Rect(screenWidth / 2, 0, 2 * screenWidth / 3, screenHeight / 4));       // 玩家2
        playerRegions.add(new Rect(3 * screenWidth / 4, 0, screenWidth, screenHeight / 4));           // 玩家3

        // 中排玩家
        playerRegions.add(new Rect(0, screenHeight / 3, screenWidth / 4, screenHeight / 2));         // 玩家4
        playerRegions.add(new Rect(3 * screenWidth / 4, screenHeight / 3, screenWidth, screenHeight / 2)); // 玩家5

        // 下排玩家
        playerRegions.add(new Rect(screenWidth / 4, 3 * screenHeight / 4, screenWidth / 3, screenHeight));  // 玩家6
        playerRegions.add(new Rect(screenWidth / 2, 3 * screenHeight / 4, 2 * screenWidth / 3, screenHeight)); // 玩家7
        playerRegions.add(new Rect(3 * screenWidth / 4, 3 * screenHeight / 4, screenWidth, screenHeight));   // 玩家8

        // 更新公共牌区域 - 设置在屏幕中央
        communityCardsRegion = new Rect(
            screenWidth / 4,
            screenHeight * 2/5,
            3 * screenWidth / 4,
            screenHeight * 3/5
        );

        // 更新底池区域 - 设置在公共牌区域下方
        potRegion = new Rect(
            screenWidth / 3,
            screenHeight * 3/5,
            2 * screenWidth / 3,
            screenHeight * 7/10
        );
    }
}